package com.tools.scanner.ad.impl

import android.app.Activity
import android.util.Log
import android.view.View
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.LoadAdError
import com.tools.scanner.QrApp

class BannerAdImpl(mAdId: String, mLocation: String) : BaseAdImpl(mAdId, mLocation) {
    private var isAdAvailable = false

    override fun loadBanner(adView: AdView, adLocation: String, adId: String) {
        adView.adUnitId = adId
        adView.setAdSize(AdSize.BANNER)
        adView.setOnPaidEventListener { adValue ->
            notifyAdPaid(adValue)
        }
        adView.adListener = object : AdListener() {
            override fun onAdLoaded() {
                val adapterName =
                    adView.responseInfo?.mediationAdapterClassName ?: ""
                notifyAdLoadEnd(-1, "", adapterName, true)
            }

            override fun onAdFailedToLoad(loadAdError: LoadAdError) {
                val errorCode = loadAdError.code
                val errorMessage = loadAdError.message
                notifyAdLoadEnd(errorCode, errorMessage, "", false)
            }

            override fun onAdClicked() {
                notifyClick()
            }

            override fun onAdClosed() {
                super.onAdClosed()
                notifyAdClose()
            }

            override fun onAdOpened() {
                super.onAdOpened()
                notifyAdShow()
            }
        }
        val adRequest = AdRequest.Builder().build()
        adView.loadAd(adRequest)
        isAdAvailable = true
    }

    override fun beginLoad(adLocation: String, adId: String) {

    }

    override fun beginShow(activity: Activity, adLocation: String, adId: String) {

    }

    override fun getNativeView(): View? {
        return null
    }

    override fun isAdAvailable(): Boolean {
        return isAdAvailable
    }
}